﻿//
// GNS_PvEAllInfo.cpp
// MobileManageSys
//FuncTion  人机对战总玩分成
// Created by mr.l on 16/1/16.
//
//
#include "GnsClientFrame.h"
#include "GNS_PvEAllInfo.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
//extern ISocketMgr * _GameSock;

GNS_PvEAllInfo::GNS_PvEAllInfo()
{
    pGNS_PvEAllInfo=0;
    TimeText=nullptr;
    ShowText=nullptr;
    GameName=nullptr;
    DateListView=nullptr;
    TypeListView=nullptr;
    Agent=nullptr;
    Promoter=nullptr;
    account=nullptr;
    accdetailed=nullptr;
    accbg=nullptr;
    pPvEAllInfoDaily=nullptr;
    typeNum=0;
    PlayerName=0;
    SendType=0;
    GameType=nullptr;
    Mon=0;
    Year=0;
    Day=0;
}

GNS_PvEAllInfo::~GNS_PvEAllInfo()
{
    
}

GNS_PvEAllInfo* GNS_PvEAllInfo::createLayer()
{
    auto pGNS_PvEAllInfo = GNS_PvEAllInfo::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_PvEAllInfo);
    return pGNS_PvEAllInfo;
}

bool GNS_PvEAllInfo::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;

	pPvEAllInfoDaily = GNS_PvEAllInfoDaily::createLayer();
	pPvEAllInfoDaily->setVisible(false);
    return true;
}
void GNS_PvEAllInfo::SetNodeName(std::string CsbName)
{
    pGNS_PvEAllInfo = CSLoader::createNode(CsbName);
    pGNS_PvEAllInfo->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_PvEAllInfo);
    addChild(pGNS_PvEAllInfo,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_PvEAllInfo);
    auto pBackBtn = static_cast<Button*>(pGNS_PvEAllInfo->getChildByName("PvEAllInfoBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::BtnCallBack, this));
    
    TimeText = static_cast<Text*>(pGNS_PvEAllInfo->getChildByName("Text_3"));
    TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));

    ShowText = static_cast<Text*>(pGNS_PvEAllInfo->getChildByName("Text_5"));
    
    auto pTimeBtn = static_cast<Button*>(pGNS_PvEAllInfo->getChildByName("Button_3"));
    pTimeBtn->setTag(2);
    pTimeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::BtnCallBack, this));
    auto typeBtn = static_cast<Button*>(pGNS_PvEAllInfo->getChildByName("TypeButton"));
    typeBtn->setTag(3);
    typeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::BtnCallBack, this));
    
    TypeListView = static_cast<ListView*>(pGNS_PvEAllInfo->getChildByName("TypeListView"));
    TypeListView->setZOrder(13);
    TypeListView->setVisible(false);
    
    DateListView = static_cast<ListView*>(pGNS_PvEAllInfo->getChildByName("ListView_1"));
//    DateListView->addEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::SelectedItemEvents, this));
    
    GameName = static_cast<Text*>(pGNS_PvEAllInfo->getChildByName("Text_4"));
    GameType = static_cast<ListView*>(pGNS_PvEAllInfo->getChildByName("GameType"));
    GameType->removeAllItems();
    
	int x = 0;
    for (auto it:(* GNS_PlayerMgr::getInstance()->getGameList())) {
        if (it.iGameType==1) {
            auto pbutton = Button::create("p0Btn.png");
			pbutton->setTitleText(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
            pbutton->setTitleColor(Color3B::BLACK);
            pbutton->setTitleFontSize(20);
            pbutton->setTag(8);
            pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::BtnCallBack, this));
            GameType->addChild(pbutton);
			++x;
			if (x == 1)
				GameName->setString(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
        }
    }
    GameType->setVisible(false);

    auto TypeAllBtn = static_cast<Button*>(TypeListView->getChildByName("Allbtn"));
    TypeAllBtn->setTag(5);
    TypeAllBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::ListViewCallBack, this));
    
    auto TypeAgentBtn = static_cast<Button*>(TypeListView->getChildByName("AgentBtn"));
    TypeAgentBtn->setTag(6);
    TypeAgentBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::ListViewCallBack, this));
    
    auto TypePromoterBtn = static_cast<Button*>(TypeListView->getChildByName("PromoterBtn"));
    TypePromoterBtn->setTag(7);
    TypePromoterBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::ListViewCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_PvEAllInfo->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_PvEAllInfo->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(3);
    typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::BtnCallBack, this));
    
    auto gamebtn = static_cast<Button*>(pGNS_PvEAllInfo->getChildByName("Button_8_1"));
    gamebtn->setTag(4);
    gamebtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::BtnCallBack, this));
    
    auto gamebtn1 = static_cast<Button*>(pGNS_PvEAllInfo->getChildByName("Button_3_0"));
    gamebtn1->setTag(4);
    gamebtn1->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::BtnCallBack, this));
    
    Agent = static_cast<ImageView*>(pGNS_PvEAllInfo->getChildByName("AgentBtn"));
    Promoter = static_cast<ImageView*>(pGNS_PvEAllInfo->getChildByName("Promoter"));
    Promoter->setVisible(false);
    account = static_cast<ImageView*>(pGNS_PvEAllInfo->getChildByName("account"));
    account->setVisible(false);
    
    TitleVec.push_back(Agent);
    TitleVec.push_back(Promoter);
    TitleVec.push_back(account);
    
    accdetailed = static_cast<Text*>(pGNS_PvEAllInfo->getChildByName("Text_9"));
    accdetailed->setString("暂无数据");
    accdetailed->setVisible(false);
    accbg = static_cast<ImageView*>(pGNS_PvEAllInfo->getChildByName("Image_11"));
    accbg->setVisible(false);
    
    typeNum = GNS_PlayerMgr::getInstance()->getUserLevel()+1;
    if (typeNum==1) {
        TypeListView->removeChild(TypeAgentBtn);
        TypeListView->removeChild(TypePromoterBtn);
        ShowText->setString(TypeAllBtn->getTitleText());
    }else if (typeNum==2)
    {
        Agent->setVisible(false);
        Promoter->setVisible(true);
        account->setVisible(false);
		TypeListView->removeChild(TypePromoterBtn);
        TypeListView->removeChild(TypeAllBtn);
		ShowText->setString(TypeAgentBtn->getTitleText());
    }else if (typeNum==3)
    {
        Agent->setVisible(false);
        Promoter->setVisible(false);
        account->setVisible(true);
        TypeListView->removeChild(TypeAllBtn);
        TypeListView->removeChild(TypeAgentBtn);
        ShowText->setString(TypePromoterBtn->getTitleText());
        DateListView->setVisible(false);
        accdetailed->setVisible(true);
        accbg->setVisible(true);
    }
    
}
void GNS_PvEAllInfo::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
                //auto RevRepNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(3);
                //RevRepNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("RevRep", true);
                break;
            }
            case 2:
            {
					  /*pCalendar =GNS_Calendar::createCalendar();
					  pCalendar->SetCalendBgImage("Calendarbg.png");
					  std::vector<Button *> CalenderBtn;
					  pCalendar->GetTimeButton()->swap(CalenderBtn);
					  for (auto & it:CalenderBtn) {
					  it->setTag(100);
					  it->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::TimeButtonCallBack, this));
					  }
					  */
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_PvEAllInfo::TimeButtonCallBack, this));
                break;
            }
            case 3:
            {
                if (TypeListView->isVisible()==true) {
                    TypeListView->setVisible(false);
                }else{
                    TypeListView->setVisible(true);
                }
                break;
            }
            case 4:
            {
                if (GameType->isVisible()==true) {
                    GameType->setVisible(false);
                }else{
                    GameType->setVisible(true);
                }
                break;
            }
            case 8:
            {
                GameName->setString(pButton->getTitleText());
                for (int i =0; i<GNS_PlayerMgr::getInstance()->getGameList()->size(); i++) {
					auto st1 = SystemMgr::getInstance()->utf8ToGb(pButton->getTitleText().c_str());
					auto st2 = GNS_PlayerMgr::getInstance()->getGameList()->at(i).szGameName;
                    if (st1 == st2) {
                        GNS_PlayerMgr::getInstance()->getPlayProRation()->clear();
                        CMD_CS_GetManToMachPlayProfitRatio pGetPlayPro;
                        pGetPlayPro._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                        pGetPlayPro._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                        pGetPlayPro._gameId=GNS_PlayerMgr::getInstance()->getGameList()->at(i).nGameId;
                        pGetPlayPro._type=typeNum;
                        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                        memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
                        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT_RATIO,(char*) &pGetPlayPro, sizeof(pGetPlayPro));
                        GameType->setVisible(false);
						GNS_PlayerMgr::getInstance()->setGameId(pGetPlayPro._gameId);
                    }
                }
                
                break;
            }
            default:
                break;
        }
    }
}

void GNS_PvEAllInfo::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        Mon = pCalendar->Mon;
        Year = pCalendar->year;
        Day = atoi(BtnText.c_str());
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
		GNS_PlayerMgr::getInstance()->getPlayProRation()->clear();
        CMD_CS_GetManToMachPlayProfitRatio pGetPlayPro;
        pGetPlayPro._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetPlayPro._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
		pGetPlayPro._gameId = GNS_PlayerMgr::getInstance()->getGameId();
        pGetPlayPro._type=typeNum;
        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT_RATIO, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
    }
}
//暂时没有用 以后应该会用到
void GNS_PvEAllInfo::ListViewCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int Btntag = pButton->getTag();
        std::string btnText = pButton->getTitleText();
        ShowText->setString(btnText);
        TypeListView->setVisible(false);
        switch (Btntag) {
            case 5:
            {
                TiteChooseType(Agent);
                SendType=0;
                break;
            }
            case 6:
            {
                TiteChooseType(Promoter);
                SendType=1;
                break;
            }
            case 7:
            {
                TiteChooseType(account);
                SendType=2;
                break;
            }
            default:
                break;
        }
        GNS_PlayerMgr::getInstance()->getPlayProRation()->clear();
        CMD_CS_GetManToMachPlayProfitRatio pGetPlayPro;
        pGetPlayPro._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetPlayPro._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
		pGetPlayPro._gameId = GNS_PlayerMgr::getInstance()->getGameId();
        pGetPlayPro._type=SendType+1;
        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT_RATIO, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
    }
}
void GNS_PvEAllInfo::TiteChooseType(cocos2d::ui::ImageView *type)
{
    for (auto &it:TitleVec) {
        if (it==type) {
            it->setVisible(true);
        }else{
            it->setVisible(false);
        }
    }
    if(type==account)
    {
        accdetailed->setVisible(true);
        accbg->setVisible(true);
        DateListView->setVisible(false);
    }else{
        accdetailed->setVisible(false);
        accbg->setVisible(false);
        DateListView->setVisible(true);
    }
}
void GNS_PvEAllInfo::AcceptSeverDate()
{
    if (SendType==0) {
        for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProRation())) {
            accdetailed->setString(StringUtils::format("%lld",it._agentRatio));
        }
    }
    DateListView->removeAllItems();
    int index =(int)GNS_PlayerMgr::getInstance()->getPlayProRation()->size();
    int x=0;
    Size bgSize;
	int level = GNS_PlayerMgr::getInstance()->getUserLevel();
	string pngname = (level == 0) ? "line between1.png" : "line2.png";
    for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProRation())) {
        auto itembg = Button::create(pngname);
        itembg->setTag(100+x);
        itembg->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfo::ListViewBtn, this));
        DateListView->addChild(itembg);
        bgSize = itembg->getContentSize();
        
        auto Name = Text::create(StringUtils::format("%d",it._lowlineCode), GameFontName, 24);
        Name->setPosition(Vec2(bgSize.width*0.142, bgSize.height*0.4));
        Name->setColor(Color3B::BLACK);
        itembg->addChild(Name);
        
        auto lineRatio = Text::create(StringUtils::format("%lld",it._lowlineRatio), GameFontName, 24);
        lineRatio->setPosition(Vec2(bgSize.width*0.48, bgSize.height*0.4));
        lineRatio->setColor(Color3B::BLACK);
        itembg->addChild(lineRatio);
        
        auto upRatio = Text::create(StringUtils::format("%lld",it._uplineRatio), GameFontName, 24);
        upRatio->setPosition(Vec2(bgSize.width*0.8, bgSize.height*0.4));
        upRatio->setColor(Color3B::BLACK);
        itembg->addChild(upRatio);
        x++;
    }
    if (bgSize.width!=0||bgSize.height!=0) {
        DateListView->setInnerContainerSize(Size(bgSize.width, (bgSize.height+10)*index));
    }
}
void GNS_PvEAllInfo::SelectedItemEvents(cocos2d::Ref *pSender, ui::ListView::EventType type)
{
    if (typeNum==1) {
        if (type==ui::ListView::EventType::ON_SELECTED_ITEM_START) {
            auto pListView = (ListView*)pSender;
            int index = (int)pListView->getCurSelectedIndex();
            int x=0;
            for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProRation())) {
                if (index==x) {
                    CMD_CS_GetManToMachPlayProfitRatio pGetPlayPro;
					pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
					pGetPlayPro._uplineCode = it._lowlineCode;
                    pGetPlayPro._gameId=GNS_PlayerMgr::getInstance()->getGameId();
                    pGetPlayPro._type=2;
                    std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                    memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT_RATIO, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
                    PlayerName=it._lowlineCode;
                    break;
                }
                x++;
            }
        }
    }
}
void GNS_PvEAllInfo::SeleLayer(int LayerNum)
{
	pGNS_PvEAllInfo->setVisible(false);
	pPvEAllInfoDaily->removeAllChildren();
	pPvEAllInfoDaily->setVisible(true);
    pPvEAllInfoDaily->SetNodeName("PvEAllInfoDilyDate.csb");
    pPvEAllInfoDaily->SetProName(StringUtils::format("%d",PlayerName));
}

void GNS_PvEAllInfo::ListViewBtn(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
		if (GNS_PlayerMgr::getInstance()->getUserLevel() < 1)
		{
			auto pButton = (Button*)pSender;
			int BtnTag = pButton->getTag();
			int x = 0;
			for (auto it : (*GNS_PlayerMgr::getInstance()->getPlayProRation())) {
				if (BtnTag - 100 == x) {
					GNS_PlayerMgr::getInstance()->getPlayProProRation()->clear();
					CMD_CS_GetManToMachPlayProfitRatio pGetPlayPro;
					pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
					pGetPlayPro._uplineCode = it._lowlineCode;
					pGetPlayPro._gameId = GNS_PlayerMgr::getInstance()->getGameId();
					pGetPlayPro._type = 2;
					std::string TimeDate = StringUtils::format("%d-%02d-%02d", Year, Mon, Day);
					memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
					GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT_RATIO,(char*) &pGetPlayPro, sizeof(pGetPlayPro));
					PlayerName = it._lowlineCode;
					break;
				}
				x++;
			}
		}
    }
}